﻿using LandManager.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LandManager.Web.Service
{
    public class AgencyService : IService<Agency>
    {
        public bool Add(Agency t)
        {
            using (DataComDataContext db = new DataComDataContext())
            {
                try
                {
                    db.Agency.InsertOnSubmit(t);
                    db.SubmitChanges();

                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public bool Delete(Agency model)
        {
            using (DataComDataContext db = new DataComDataContext())
            {
                try
                {
                    var q = from c in db.Agency
                            where c.agency_ID == model.agency_ID
                            select c;
                    db.Agency.DeleteOnSubmit(q.First());
                    db.SubmitChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public bool Edit(Agency t)
        {
            using (DataComDataContext db = new DataComDataContext())
            {
                try
                {
                    var q = from c in db.Agency
                            where c.agency_ID == t.agency_ID
                            select c;

                    Agency newAgency = q.First();
                    newAgency.agency_name = t.agency_name;
                    newAgency.agency_Tel = t.agency_Tel;
                    newAgency.agency_Address = t.agency_Address;

                    db.SubmitChanges();

                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public List<Agency> GetList()
        {
            using (DataComDataContext db = new DataComDataContext())
            {
                var q = from c in db.Agency
                        select c;
                if (q.Count() > 0)
                {
                    return q.ToList();
                }
                else
                {
                    return null;
                }
            }
        }

        public Agency FindByID(int id)
        {
            using (DataComDataContext db = new DataComDataContext())
            {
                var q = from c in db.Agency
                        where c.agency_ID == id
                        select c;
                if (q.Count() > 0)
                {
                    return q.FirstOrDefault();
                }
                else
                {
                    return null;
                }
            }
        }

        public Agency FindByName(string name)
        {
            using (DataComDataContext db = new DataComDataContext())
            {
                var q = from c in db.Agency
                        where c.agency_name == name
                        select c;
                if (q.Count() > 0)
                {
                    return q.FirstOrDefault();
                }
                else
                {
                    return null;
                }
            }
        }
    }
}